<?php

/* * 
 * 菜单
 */
namespace Admin\Model;
use Common\Model\CommonModel;


/**
 * 文章模型类
 * Class AuthGroupModel
 */
class ModelModel extends CommonModel {

    //自动验证
    protected $_validate = array(
        //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
        array('name', 'require', '模型名称不能为空！', 1, 'regex', CommonModel:: MODEL_BOTH ),
    		array('model', 'require', '模型不能为空！', 1, 'regex', CommonModel:: MODEL_BOTH ),

    	
    		array('status', array(0, 1), '状态错误，状态只能是1或者0！', 2, 'in'),
    );
    //自动完成
    protected $_auto = array(
            //array(填充字段,填充内容,填充条件,附加规则)
    );



    /**
     * 更新缓存
     * @param type $data
     * @return type
     */
    public function data_cache($data = null) {
      
            $data = $this->select();
            
            foreach ($data as $key=>$v){
            	
            	$result[$v[model]]=$v;
            	
            }
            F("Model", $result);
      
    }
    public function _after_insert() {
    	parent::_after_insert();
    	$this->data_cache();
    }
    /**
     * 后台有更新/编辑则删除缓存
     * @param type $data
     */	
    public function _after_write(&$data) {
        parent::_after_write($data);
       $this->data_cache();
    }

    	

    //删除操作时删除缓存
    public function _after_delete($data, $options) {
        parent::_after_delete($data, $options);
        
      
      	
      	  $this->data_cache();
    
    }
    /**
     * 获取模板选择项
     * @param type $name 服务类型
     * @param type $params 参数
     * @return type
     */
  public  function  select_tpls($model,$pix,$value){
    
    
    
    	$filepath = TEMPLATE_PATH .C('THEME') . "/Home/" .$model."/";
    	//echo $model;
    	$tp_show = str_replace($filepath , "", glob($filepath . $pix.'*'));
    
    
    	foreach ($tp_show as $k => $v) {
    
    		$f=fopen($filepath.$v,"r");
    
    		$name=substr_between(fgets ($f),"@","@");
    
    
    		fclose ($f);
    		$t[file]=str_replace('.html',"",$v);
    		$t[name]=$name?$name:$t[file];
    		
    
    		$result[]=$t;
    
    	}
    
    	unset($tp_show);
    
    	if($value){
    
    		foreach ($result as $key => $v) {
    
    			if($v[file]===$value)
    				$result[$key][select] =  'selected' ;
    
    		}
    	}
    
    	return $result;
    
    }

}

?>